<?php

/**
 * home actions.
 *
 * @package    Web20
 * @subpackage home
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class homeActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  	public function executeIndex(sfWebRequest $request)
  	{
		$this->categorias = CategoryTable::getCategories();
		$this->articulos = ProductTable::getProductsForHome();
  	}

	public function executeSearch(sfWebRequest $request)
	{
		$this->categorias = CategoryTable::getCategories();
		/*
		$this->forwardUnless($searchString = $request->getParameter('search'), 'home', 'index');
		$this->products = Doctrine_Core::getTable('Product')->getForLuceneQuery($searchString);
		*/

		$searchOptions = array();

		if (!$request->getParameter("new")) {
			$searchOptions['name'] = $this->getUser()->getAttribute('search.name');
			$searchOptions['description'] = $this->getUser()->getAttribute('search.description');
			$searchOptions['category_id'] = $this->getUser()->getAttribute('search.category_id');
		} else {
			// se resetean los parametros de busquedas anteriores
			$this->getUser()->setAttribute('search.name', null);
			$this->getUser()->setAttribute('search.description', null);
			$this->getUser()->setAttribute('search.category_id', null);

			// se setean los parametros de busqueda para la paginacion
			if ($string = $request->getParameter('search_string')) {
				$searchOptions['name'] = $string;
				$searchOptions['description'] = $string;
				$this->getUser()->setAttribute('search.name', $searchOptions['name']);
				$this->getUser()->setAttribute('search.description', $searchOptions['description']);
			}
			if ($category = $request->getParameter('category')) {
				$searchOptions['category_id'] = $category;
				$this->getUser()->setAttribute('search.category_id', $searchOptions['category_id']);
			}
		}
		$this->pager = new sfDoctrinePager('Product', 8);
		$this->pager->setQuery(ProductTable::getSearchQuery($searchOptions));
		$this->pager->setPage($request->getParameter('page', 1));
		$this->pager->init();
	}

	public function executeTag(sfWebRequest $request)
	{
		$this->categorias = CategoryTable::getCategories();

		$this->forward404Unless($request->hasParameter('name'), "Debe enviar el parámetro 'name'.");
		$tag = $request->getParameter('name');

		$q = PluginTagTable::getObjectTaggedWithQuery('Product', $tag);

		$this->pager = new sfDoctrinePager('Tag', 8);
		$this->pager->setQuery($q);
		$this->pager->setPage($request->getParameter('page', 1));
		$this->pager->init();
	}
}
